package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;

import com.example.demo.entity.Facility;


import com.example.demo.mapper.FacilityMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/facility")
@CrossOrigin
public class FacilityController {

    @Resource
    FacilityMapper facilityMapper;


    @PostMapping
    public Result<?> save(@RequestBody Facility facility){
        facilityMapper.insert(facility);
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody Facility facility){
        facilityMapper.updateById(facility);
        return Result.success();
    }

    @DeleteMapping("/{fid}")
    public Result<?> delete(@PathVariable Long fid) {

        facilityMapper.deleteById(fid);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(required = false) Integer id){
        LambdaQueryWrapper<Facility> wrapper = Wrappers.<Facility>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Facility::getFname,search);
        }
        if (id != null) {
            wrapper.eq(Facility::getFid,id);
        }

        Page<Facility> facilityPage=facilityMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(facilityPage);
    }


}

